package org.wupgame.controller;

import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.*;
import org.wupgame.model.Player;
import org.wupgame.model.Room;
import org.wupgame.model.RoomState;
import org.wupgame.model.RoomStateMsg;
import org.wupgame.model.dto.ActionDTO;
import org.wupgame.model.dto.RoomDTO;
import org.wupgame.service.GameService;
import org.wupgame.service.e.GameException;

@RequestMapping("/game")
@RestController
public class GameController {
	@Resource
	private GameService gameService;
	
	@PostMapping("/create")
	public ResponseEntity<?> createRoom(@RequestBody RoomDTO createRoomDTO) {
		RoomState roomState;
		try {
			roomState = gameService.createNewGame(
				createRoomDTO.getPlayer(),
				createRoomDTO.getGameName(),
				createRoomDTO.getRoomPassword());
		}
		catch (GameException e) {
			return ResponseEntity
				.badRequest()
				.body(e.getMessage());
		}
		catch (JpaSystemException e) {
			return ResponseEntity
				.badRequest()
				.body("JPA系统错误，房间信息无法录入数据库！");
		}
		catch (Exception e) {
			return ResponseEntity
				.badRequest()
				.body("Radis错误，房间信息无法录入服务器内存！");
		}
		return ResponseEntity
			.status(200)
			.body(new RoomStateMsg(roomState, createRoomDTO.getPlayer().getId()));
	}
	
	@PostMapping("/join")
	public ResponseEntity<?> joinRoom(@RequestBody RoomDTO roomDTO) {
		RoomState roomState;
		try {
			roomState = gameService.joinGame(
				roomDTO.getRoomId(),
				roomDTO.getPlayer(),
				roomDTO.getRoomPassword());
		}
		catch (GameException e) {
			return ResponseEntity
				.badRequest()
				.body(e.getMessage());
		}
		catch (JpaSystemException e) {
			return ResponseEntity
				.badRequest()
				.body("JPA系统错误，房间信息无法录入数据库！");
		}
		catch (Exception e) {
			return ResponseEntity
				.badRequest()
				.body("Radis错误，房间信息无法录入服务器内存！");
		}
		return ResponseEntity
			.status(200)
			.body(new RoomStateMsg(roomState, roomDTO.getPlayer().getId()));
	}
	
	@PostMapping("/start")
	public ResponseEntity<?> startRoom(@RequestBody RoomDTO roomDTO) {
		RoomState roomState;
		try {
			roomState = gameService.startGame(
				roomDTO.getRoomId(),
				roomDTO.getPlayer());
		}
		catch (GameException e) {
			return ResponseEntity
				.badRequest()
				.body(e.getMessage());
		}
		catch (JpaSystemException e) {
			return ResponseEntity
				.badRequest()
				.body("JPA系统错误，房间信息无法录入数据库！");
		}
		catch (Exception e) {
			return ResponseEntity
				.badRequest()
				.body("Radis错误，房间信息无法录入服务器内存！");
		}
		return ResponseEntity
			.status(200)
			.body(new RoomStateMsg(roomState, roomDTO.getPlayer().getId()));
	}
	
	@PostMapping("/process")
	public ResponseEntity<?> processRoom(@RequestBody ActionDTO actionDTO) {
		RoomStateMsg roomStateOnMessage;
		try {
			roomStateOnMessage = gameService.processPlayerAction(actionDTO);
		}
		catch (GameException e) {
			return ResponseEntity
				.badRequest()
				.body(e.getMessage());
		}
		catch (Exception e) {
			return ResponseEntity
				.badRequest()
				.body("Radis错误，房间信息无法录入服务器内存！");
		}
		return ResponseEntity
			.status(200)
			.body(roomStateOnMessage);
	}
	
	@PostMapping("/leave")
	public ResponseEntity<?> leaveRoom(@RequestBody RoomDTO roomDTO) {
		try {
			gameService.leaveGame(
				roomDTO.getRoomId(),
				roomDTO.getPlayer());
		}
		catch (GameException e) {
			return ResponseEntity
				.badRequest()
				.body(e.getMessage());
		}
		catch (JpaSystemException e) {
			return ResponseEntity
				.badRequest()
				.body("JPA系统错误，房间信息无法录入数据库！");
		}
		catch (Exception e) {
			return ResponseEntity
				.badRequest()
				.body("Radis错误，房间信息无法录入服务器内存！");
		}
		return ResponseEntity
			.status(200)
			// 玩家离开游戏后，房间中不存在该玩家消息，所以这里直接返回空
			.body(null);
	}
	
	@PostMapping("/current")
	public ResponseEntity<?> currentGame(
		@RequestBody
		Player player) {
		RoomState roomState;
		try {
			roomState = gameService.getPlayerGames(player);
		}
		catch (GameException e) {
			return ResponseEntity
				.badRequest()
				.body(e.getMessage());
		}
		catch (JpaSystemException e) {
			return ResponseEntity
				.badRequest()
				.body("JPA系统错误，无法查询信息！");
		}
		catch (Exception e) {
			return ResponseEntity
				.badRequest()
				.body("Radis错误，无法查询信息！");
		}
		return ResponseEntity
			.status(200)
			.body(new RoomStateMsg(roomState, player.getId()));
	}
	
	@GetMapping("/available")
	public ResponseEntity<?> getAvailableGames(@RequestParam int pageNum) {
		Page<Room> rooms;
		try {
			rooms = gameService.getAvailableGames(pageNum);
		}
		catch (JpaSystemException e) {
			return ResponseEntity
				.badRequest()
				.body("JPA系统错误，无法查询信息！");
		}
		catch (Exception e) {
			return ResponseEntity
				.badRequest()
				.body("未知错误，无法查询信息！");
		}
		return ResponseEntity
			.status(200)
			.body(rooms);
	}
}
